//
//  LFPhotoPicker.m
//  AntMarket
//
//  Created by LF on 2017/3/22.
//  Copyright © 2017年 LF. All rights reserved.
//

#import "LFPhotoPicker.h"
#import <BlocksKit/BlocksKit.h>
#import <MobileCoreServices/MobileCoreServices.h>

@interface LFPhotoPicker ()
<
UINavigationControllerDelegate,
UIImagePickerControllerDelegate
>

//**  选择的图片数组 */
@property (nonatomic, strong) NSMutableArray *selectedPhotos;

@end

@implementation LFPhotoPicker

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.selectedPhotos = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void) lf_selectePhotoInVC:(UIViewController *)showVC showCamera:(BOOL)showCamera limitCount:(NSInteger)limitCount result:(PhotoSelectBlock)result
{
    self.resultBlock = result;
    
    if (showCamera) {
        UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"选择图片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *a1 = [UIAlertAction actionWithTitle:@"摄像头" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            UIImagePickerController *imagePicker= [[UIImagePickerController alloc] init];
            imagePicker.delegate = self;
            imagePicker.allowsEditing= YES;
            imagePicker.sourceType= UIImagePickerControllerSourceTypeCamera;
            [showVC presentViewController:imagePicker animated:YES completion:^{
                
            }];
        }];
        UIAlertAction *a2 = [UIAlertAction actionWithTitle:@"本地图片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self showPhotoPickerToVC:showVC limitCount:limitCount resultCallBack:^(NSArray *selectedPhotos) {
                result(selectedPhotos);
            }];
        }];
        [ac addAction:a1];
        [ac addAction:a2];
        [CURRENT_NAV.topViewController presentViewController:ac animated:YES completion:nil];
    

    } else {
        [self showPhotoPickerToVC:showVC limitCount:limitCount resultCallBack:^(NSArray *selectedPhotos) {
            result(selectedPhotos);
        }];
    }
}


// TODO 这里的代理老不走呢? ffffffuck
#pragma UIImagePickerController Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(__bridge NSString *)kUTTypeImage]) {
        UIImage *img= [info objectForKey:UIImagePickerControllerEditedImage];
        self.resultBlock(@[img]);
    }
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}


//
- (void) showPhotoPickerToVC:(UIViewController *)rootVC limitCount:(NSInteger)limitCount resultCallBack:(PhotoSelectBlock)resultBack
{
    [self.selectedPhotos removeAllObjects];
    
    ZLPhotoPickerViewController *pickerVc = [[ZLPhotoPickerViewController alloc] init];
    // MaxCount, Default = 9
    //    pickerVc.maxCount = 9 - self.assets.count;
    pickerVc.maxCount = limitCount;
    // Jump AssetsVc
    pickerVc.status = PickerViewShowStatusCameraRoll;
    // Recoder Select Assets
    pickerVc.selectPickers = self.selectedPhotos;
    // Filter: PickerPhotoStatusAllVideoAndPhotos, PickerPhotoStatusVideos, PickerPhotoStatusPhotos.
    pickerVc.photoStatus = PickerPhotoStatusPhotos;
    // Desc Show Photos, And Suppor Camera
    pickerVc.topShowPhotoPicker = YES;
    
    pickerVc.isShowCamera = YES;
    // CallBack
    pickerVc.callBack = ^(NSArray<ZLPhotoAssets *> *status){
        
        NSMutableArray *photos = [NSMutableArray arrayWithCapacity:status.count];
        for (ZLPhotoAssets *assets in status) {
            ZLPhotoPickerBrowserPhoto *photo = [ZLPhotoPickerBrowserPhoto photoAnyImageObjWith:assets];
            photo.asset = assets;
            [photos addObject:photo.photoImage];
        }
        
        self.selectedPhotos = photos;
        
        resultBack(self.selectedPhotos);
        
    };
    [pickerVc showPickerVc:rootVC];
}


@end
